$(function(){

    var str = decodeURI(location.search);
    var arr = str.slice(1).split("&");
    var id = +arr[0].split("=")[1];
    var title = arr[1].split("=")[1];

    function render(){
        $.ajax({
            url: "http://127.0.0.1:9090/api/getcouponproduct",
            data: { couponid: id },
            dataType: "json",
            success: function (result) {
                console.log(result);
                $(".mm-product").html(template("temp-list", result));
            }
        });
    }
    render();

    var ul;

    $.ajax({
        url: "http://127.0.0.1:9090/api/getcouponproduct",
        data: { couponid: id },
        dataType: "json",
        success: function (result) {
            console.log(result);
            result.result[0].title = title;
            $(".top").html(template("temp-title",result));
            $(".img").html(template("temp-img",result));
            list = $(".img").children("li");
            setUl(list);

            ul = $(".img")[0];
            console.log(ul);
        }
    });

    var index = 0;

    // 点击显示模态框
    $(".mm-product").on("click",".arrow",function(){
        $(".modal").css("display","block");
    });

    // 点击移动上一张
    $(".prev").on("click",function(){
        if(index <= 0){
            return;
        }
        index--;
        ul.style.top = -index * 190 + "px";
    });

    // 点击移动到下一张
    $(".next").on("click", function(){
        index++;
        ul.style.top = -index * 190 + "px";
    });

    // 动态设置ul的高度
    function setUl(list){
        var count = 0;
        for(var i = 0; i < list.length; i++){
            count += i;
        }
    }

});